package com.sample.springbatch.repository;

import com.sample.springbatch.model.User;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.LockModeType;

/**
 * Created by zwg on 2017/8/9.
 */
public interface UserRepository extends PagingAndSortingRepository<User,Integer> {

    @Transactional
    @Modifying
    @Query(value = "update User u set u.age=u.age+1 where u.id=:id and u.age<10")
    int updateUserAge(@Param(value = "id") Integer id);


    @Transactional
    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    User findById(Integer id);

}
